//
//  M_CartListPayView.swift
//  letuofu
//
//  Created by dooland2 on 2020/6/4.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import UIKit
import CoreText

class M_CartListPayView: UIView {
    //定义一个闭包类型
    typealias payBlock = (_ type:Int) -> Void
    //声明一个变量
    var payBtnBack: payBlock?
    //定义一个方法,方法的参数为和swiftBlock类型一致的闭包,并赋值给callBack
    func callPayBlock(block: @escaping payBlock) {
        payBtnBack = block
    }

    var productInfo = [[String:Any?]]()
    let closeAllBtn = Button()
    let closeBtn = Button()
    let cottonView = UIView()
    let mainView = UIView()
    let ZYLab = UILabel()
    let JDLab = UILabel()
    
    var ZYAry = [[String:Any?]]()
    var JDAry = [[String:Any?]]()
    
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
        drawView(view: closeAllBtn, { (make) in
            make.left.right.top.bottom.equalToSuperview()
        })
        closeAllBtn.backgroundColor = .init(white: 0, alpha: 0.8)
        closeAllBtn.addTarget(self, action: #selector(closeAllBtnClick(btn:)), for: .touchUpInside)
        
        drawView(view: mainView, { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(self.snp.bottom)
            make.height.equalTo(305)
        })
        mainView.backgroundColor = .white

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute:{
            self.showMainView()
        })
        
        mainView.drawLabel(text: "请分开结算以下商品", fontSize: 15, textColor: .x333333, { (make) in
            make.top.equalToSuperview().offset(17)
            make.centerX.equalToSuperview()
        })
        
        mainView.drawLabel(text: "自营专区", fontSize: 13, textColor: .x666666, { (make) in
            make.top.equalToSuperview().offset(55)
            make.left.equalToSuperview().offset(22)
        })
        
        mainView.drawLabel(label: ZYLab,text: "", fontSize: 15, textColor: .x666666, { (make) in
            make.top.equalToSuperview().offset(78)
            make.left.equalToSuperview().offset(22)
        })
        
        var paybtn = mainView.drawButton(title: "去结算", titleColor: .xFC4C02, fontSize: 15, { (make) in
            make.top.equalToSuperview().offset(58)
            make.right.equalToSuperview().offset(-28)
            make.width.equalTo(68)
            make.height.equalTo(30)
            make.left.greaterThanOrEqualTo(ZYLab.snp.right).offset(15)
        })
        ZYLab.lineBreakMode = .byCharWrapping
        ZYLab.numberOfLines = 0
        
        // 设置边框
        paybtn.layer.borderWidth = 1
        paybtn.layer.borderColor = UIColor.xFC4C02.cgColor
        paybtn.backgroundColor = .clear
        paybtn.layer.cornerRadius = 5.0
        paybtn.addTarget(self, action: #selector(payAction(btn:)), for: .touchUpInside)
        paybtn.tag = 105001
        
        mainView.drawLabel(text: "京东配送", fontSize: 13, textColor: .x666666, { (make) in
            make.top.equalToSuperview().offset(138)
            make.left.equalToSuperview().offset(22)
        })
        
        paybtn = mainView.drawButton(title: "去结算", titleColor: .xFC4C02, fontSize: 15, { (make) in
            make.top.equalToSuperview().offset(141)
            make.right.equalToSuperview().offset(-28)
            make.width.equalTo(68)
            make.height.equalTo(30)
        })
        // 设置边框
        paybtn.layer.borderWidth = 1
        paybtn.layer.borderColor = UIColor.xFC4C02.cgColor
        paybtn.backgroundColor = .clear
        paybtn.layer.cornerRadius = 5.0
        paybtn.addTarget(self, action: #selector(payAction(btn:)), for: .touchUpInside)
        paybtn.tag = 105002
        
        mainView.drawLabel(label: JDLab,text: "", fontSize: 15, textColor: .x666666, { (make) in
            make.top.equalToSuperview().offset(161)
            make.left.equalToSuperview().offset(22)
            make.right.equalTo(paybtn.snp.left).offset(-15)
        })
        JDLab.lineBreakMode = .byCharWrapping
        JDLab.numberOfLines = 0
        
        let btn = mainView.drawButton(title: "关闭", titleColor: .white, fontSize: 15) { (make) in
            make.bottom.equalToSuperview().offset(-35)
            make.centerX.equalToSuperview()
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-30)
            make.height.equalTo(35)
        }
        btn.backgroundColor = .xFC4C02
        btn.layer.cornerRadius = 7
        btn.addTarget(self, action: #selector(closeAllBtnClick(btn:)), for: .touchUpInside)
        
        var ZYSum = 0.0
        var JDSum = 0.0
        
        var ZYcentUnitName = ""
        var JDcentUnitName = ""
        var ZYcount = 0
        var JDcount = 0
        
        for dic in productInfo {
            if dic.getString("zoneCode") == "105002" {
                JDAry.append(dic)
                let p = anyToDouble(dic["productPrice"]!) ?? 0
                let n = anyToDouble(dic["productNum"]!) ?? 0
                JDcount += Int(n)
                JDSum += p*n
                JDcentUnitName = dic.getString("centUnitName") ?? ""
            }
            else if dic.getString("zoneCode") == "105001" {
                ZYAry.append(dic)
                let p = anyToDouble(dic["productPrice"]!) ?? 0
                let n = anyToDouble(dic["productNum"]!) ?? 0
                ZYcount += Int(n)
                ZYSum += p*n
                ZYcentUnitName = dic.getString("centUnitName") ?? ""
            }
        }
        
        let ZYcountStr = "共" + anyToString(ZYcount)! + "件，合计："
        let ZYcountStrdic = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14),
                         NSAttributedString.Key.foregroundColor:UIColor.x666666]
        
        let ZYSumStrdic = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14),
                         NSAttributedString.Key.foregroundColor:UIColor.xFC4C02]

        let ZYattrStr = NSMutableAttributedString(string: ZYcountStr, attributes: ZYcountStrdic)
        ZYattrStr.append(NSAttributedString(string: anyToString(ZYSum) ?? "0.0", attributes: ZYSumStrdic))
        ZYattrStr.append(NSAttributedString(string: " "+ZYcentUnitName, attributes: ZYcountStrdic))
        ZYLab.attributedText = ZYattrStr

        let JDcountStr = "共" + anyToString(JDcount)! + "件，合计："
        let JDcountStrdic = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14),
                         NSAttributedString.Key.foregroundColor:UIColor.x666666]
        
        let JDSumStrdic = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14),
                         NSAttributedString.Key.foregroundColor:UIColor.xFC4C02]

        let JDattrStr = NSMutableAttributedString(string: JDcountStr, attributes: JDcountStrdic)
        JDattrStr.append(NSAttributedString(string: anyToString(JDSum) ?? "0.0", attributes: JDSumStrdic))
        JDattrStr.append(NSAttributedString(string: " "+JDcentUnitName, attributes: JDcountStrdic))
        JDLab.attributedText = JDattrStr
    }

    func showMainView() {
        UIView.animate(withDuration: 0.4) {
            // 平移
            self.mainView.transform = CGAffineTransform(translationX: 0, y: -self.mainView.frame.height)
        }
    }
    
    @objc func closeAllBtnClick(btn: Button) {
        UIView.animate(withDuration: 0.4, animations: {
            self.mainView.transform = CGAffineTransform(translationX: 0, y: 0)
        }, completion: { _ in
            self.removeFromSuperview()
        })
    }
    
    @objc func payAction(btn: Button) {
        self.removeFromSuperview()
        if payBtnBack != nil {
            payBtnBack!(btn.tag)
        }
    }
}
